import json
import os
import csv

from tqdm import tqdm


if __name__ == '__main__':
    db_path = 'db/长安'
    ssid_list = os.listdir(db_path)
    kid_coll = []
    for ssid in tqdm(ssid_list):
        kids = os.listdir(os.path.join(db_path, ssid))
        for k in kids:
            kid = k.split('.')[0]
            kid_path = os.path.join(db_path, ssid, k)
            with open(kid_path, 'r', encoding='utf-8') as fp:
                js = json.load(fp)
            rst = js.get('result')
            report_date = rst.get('reportdate')
            if report_date > '2021-12-31':
                brand_id = rst.get('brandid')
                brand_name = rst.get('brandname')
                series_id = rst.get('seriesid')
                series_name = rst.get('seriesname')
                spec_id = rst.get('specid')
                spec_name = rst.get('specname')
                content = rst.get('content')
                car_info = rst.get('carinfo')
                bought_price = car_info.get('boughtprice')
                bought_date = car_info.get('boughtdate')
                bought_addr = car_info.get('boughtaddress')
                is_battery = car_info.get('isbattery')
                driven_kiloms = car_info.get('drivenkiloms')
                kid_coll.append((kid, report_date, 
                                 brand_id, brand_name,
                                 series_id, series_name,
                                 spec_id, spec_name,
                                 content,
                                 bought_price, bought_date, bought_addr, is_battery, driven_kiloms))
    with open('autohome_koubei_长安_2024.csv', 'w', newline='', encoding='utf-8') as fp:
        csv_writer = csv.writer(fp)
        csv_writer.writerow(('kid', 'report_date',
                             'brand_id', 'brand_name', 
                             'series_id', 'series_name',
                             'spec_id', 'spec_name',
                             'content',
                             'bought_price', 'bought_date', 'bought_addr', 'is_battery', 'driven_kiloms'))
        csv_writer.writerows(kid_coll)